#include <app_rgb.h>
#include "board.h"


struct Rgb_s_pin
{
    uint8_t LED_R_pin;
    uint8_t LED_B_pin;
    uint8_t LED_G_pin;
}; // 定义一个 RGB 结构体


struct Rgb_s_pin Rgb_pin;



static void RGB_Init(void);
static void RGB_CtrlTestFuncEntry(void *parameter);
static void RGB_CtrlTest(void);


/*
 * Initialize the RGB LED
 * */
static void RGB_Init(void)
{
    // Get rgb led pin
    Rgb_pin.LED_R_pin = rt_pin_get("PE.1");
    Rgb_pin.LED_G_pin = rt_pin_get("PE.4");
    Rgb_pin.LED_B_pin = rt_pin_get("PA.1");

    // Set the pin as output method
    rt_pin_mode(Rgb_pin.LED_R_pin, PIN_MODE_OUTPUT);
    rt_pin_mode(Rgb_pin.LED_G_pin, PIN_MODE_OUTPUT);
    rt_pin_mode(Rgb_pin.LED_B_pin, PIN_MODE_OUTPUT);
}

//RGB red led control
void RGB_Red_Ctrl(rt_bool_t on_off)
{
    if (on_off) {
        rt_pin_write(Rgb_pin.LED_R_pin, PIN_LOW);
    }else {
        rt_pin_write(Rgb_pin.LED_R_pin, PIN_HIGH);
    }
}
//RGB blue led control
void RGB_Blue_Ctrl(rt_bool_t on_off)
{
    if (on_off) {
        rt_pin_write(Rgb_pin.LED_B_pin, PIN_LOW);
    }else {
        rt_pin_write(Rgb_pin.LED_B_pin, PIN_HIGH);
    }
}
//RGB green led control
void RGB_Green_Ctrl(rt_bool_t on_off)
{
    if (on_off) {
        rt_pin_write(Rgb_pin.LED_G_pin, PIN_LOW);
    }else {
        rt_pin_write(Rgb_pin.LED_G_pin, PIN_HIGH);
    }
}

/*Task Function*/
static void RGB_CtrlTestFuncEntry(void *parameter)
{
    RGB_Init();

    RGB_Blue_Ctrl(0);
    RGB_Green_Ctrl(0);
    RGB_Red_Ctrl(0);

    while(true)
    {
        RGB_Red_Ctrl(0);
        RGB_Blue_Ctrl(1);
        rt_thread_mdelay(1000);
        RGB_Blue_Ctrl(0);
        RGB_Green_Ctrl(1);
        rt_thread_mdelay(1000);
        RGB_Green_Ctrl(0);
        RGB_Red_Ctrl(1);
        rt_thread_mdelay(1000);
    }
}

static void RGB_CtrlTest(void)
{
    rt_thread_t rgb_thread;

    rgb_thread = rt_thread_create("RGB Blink",     /*线程名字*/
                                   RGB_CtrlTestFuncEntry,/*线程入口函数*/
                                   RT_NULL,/*线程入口函数参数*/
                                   512,    /*线程栈大小*/
                                   10,     /*线程优先级*/
                                   10);    /*线程时间片*/

    if(rgb_thread != RT_NULL)
    {
        rt_thread_startup (rgb_thread);
    }
    else
    {
        rt_kprintf("RGB Thread Create Failed!");
    }
}

#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(RGB_CtrlTest, RGB control);
#endif


